// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.ui;

import zombie.Lua.LuaManager;
import zombie.core.textures.Texture;

public final class GenericButton extends UIElement {
    public boolean clicked = false;
    public UIElement MessageTarget;
    public boolean mouseOver = false;
    public String name;
    public String text;
    Texture UpTexture = null;
    Texture DownTexture = null;
    private UIEventHandler MessageTarget2 = null;

    public GenericButton(UIElement messages, float x, float y, float width, float height, String _name, String _text, Texture UpTex, Texture DownTex) {
        this.x = (double)x;
        this.y = (double)y;
        this.MessageTarget = messages;
        this.name = _name;
        this.text = _text;
        this.width = width;
        this.height = height;
        this.UpTexture = UpTex;
        this.DownTexture = DownTex;
    }

    public GenericButton(UIEventHandler messages, float x, float y, float width, float height, String _name, String _text, Texture UpTex, Texture DownTex) {
        this.x = (double)x;
        this.y = (double)y;
        this.MessageTarget2 = messages;
        this.name = _name;
        this.text = _text;
        this.width = width;
        this.height = height;
        this.UpTexture = UpTex;
        this.DownTexture = DownTex;
    }

    @Override
    public Boolean onMouseDown(double x, double y) {
        if (!this.isVisible()) {
            return Boolean.FALSE;
        } else {
            if (this.getTable() != null && this.getTable().rawget("onMouseDown") != null) {
                Object[] object = LuaManager.caller.pcall(LuaManager.thread, this.getTable().rawget("onMouseDown"), new Object[]{this.table, x, y});
            }

            this.clicked = true;
            return Boolean.TRUE;
        }
    }

    @Override
    public Boolean onMouseMove(double dx, double dy) {
        if (this.getTable() != null && this.getTable().rawget("onMouseMove") != null) {
            Object[] object = LuaManager.caller.pcall(LuaManager.thread, this.getTable().rawget("onMouseMove"), new Object[]{this.table, dx, dy});
        }

        this.mouseOver = true;
        return Boolean.TRUE;
    }

    @Override
    public void onMouseMoveOutside(double dx, double dy) {
        if (this.getTable() != null && this.getTable().rawget("onMouseMoveOutside") != null) {
            Object[] object = LuaManager.caller.pcall(LuaManager.thread, this.getTable().rawget("onMouseMoveOutside"), new Object[]{this.table, dx, dy});
        }

        this.clicked = false;
        this.mouseOver = false;
    }

    @Override
    public Boolean onMouseUp(double x, double y) {
        if (this.getTable() != null && this.getTable().rawget("onMouseUp") != null) {
            Object[] object = LuaManager.caller.pcall(LuaManager.thread, this.getTable().rawget("onMouseUp"), new Object[]{this.table, x, y});
        }

        if (this.clicked) {
            if (this.MessageTarget2 != null) {
                this.MessageTarget2.Selected(this.name, 0, 0);
            } else {
                this.MessageTarget.ButtonClicked(this.name);
            }
        }

        this.clicked = false;
        return Boolean.TRUE;
    }

    @Override
    public void render() {
        if (this.isVisible()) {
            boolean _boolean = false;
            if (this.clicked) {
                this.DrawTextureScaled(this.DownTexture, 0.0, 0.0, this.getWidth(), this.getHeight(), 1.0);
                this.DrawTextCentre(this.text, this.getWidth() / 2.0, 1.0, 1.0, 1.0, 1.0, 1.0);
            } else {
                this.DrawTextureScaled(this.UpTexture, 0.0, 0.0, this.getWidth(), this.getHeight(), 1.0);
                this.DrawTextCentre(this.text, this.getWidth() / 2.0, 1.0, 1.0, 1.0, 1.0, 1.0);
            }

            super.render();
        }
    }
}
